﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Net.Mail;
using System.Configuration;
using System.Web.Configuration;
using System.Net.Configuration;

namespace Transport.Admin
{
    public partial class TraLoiThacMac : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void SendMail(string toEmail)
        {
            Configuration configurationFile = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
            MailSettingsSectionGroup mailSettings = configurationFile.GetSectionGroup("system.net/mailSettings") as MailSettingsSectionGroup;

            String fromEmail = mailSettings.Smtp.Network.UserName;
            string host = mailSettings.Smtp.Network.Host;
            int port =  mailSettings.Smtp.Network.Port;
            string password = mailSettings.Smtp.Network.Password;
            MailMessage message = new MailMessage(fromEmail, toEmail);
            message.Subject = txt_Subject.Text;
            message.Body = "Kính chào quý khách,\nĐây là thư trả lời thắc mắc từ HK Transport\n" + txt_NoiDung.Text +"\nChúng tôi rất hân hạnh được phục vụ quý khách.\nTrung tâm hỗ trợ HK Transport";

            SmtpClient smtp = new SmtpClient(host,port);
            smtp.EnableSsl = true;
            smtp.Credentials = new NetworkCredential(fromEmail, password);
            smtp.Send(message);
        }

        protected void btn_Submit_Click(object sender, EventArgs e)
        {
            try
            {
                string address = txt_RepEmail.Text;
                if (address != "")
                {
                    SendMail(address);
                    lbThongBao.Text = "";
                }
                else
                {
                    lbThongBao.Text = "Vui lòng nhập email người nhận.";
                }
            }
            catch (Exception exp) 
            {
                lbThongBao.Text = exp.Message;
            }
        }

        protected void btn_Update_Click(object sender, EventArgs e)
        {
            try
            {
                if (GridView_HD.Rows.Count != 0)
                {
                    HoiDapControl ctrl = new HoiDapControl();

                    foreach (GridViewRow row in GridView_HD.Rows)
                    {
                        CheckBox cb = (CheckBox)row.FindControl("cb_Select");
                        if (cb != null && cb.Checked)
                        {
                            string id = GridView_HD.Rows[row.RowIndex].Cells[2].Text;
                            ctrl.updateStatus(id, "1");
                        }
                    }
                }

                Response.Redirect(HttpContext.Current.Request.Url.AbsoluteUri);
            }
            catch (Exception)
            {

            }
        }
    }
}